package com.meixiang.adapter.myFundAdapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.entity.fund.FundAccountBean;
import com.meixiang.recyclerview.RecyclerAdapter;
import com.meixiang.util.AbStrUtil;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 我的基金--历史持有基金
 */
public class FundHoldHistoryAdapter extends RecyclerAdapter<FundHoldHistoryAdapter.MyViewHolder> {

    private Context mContext;
    private List<FundAccountBean.FundHoldingList> fund_holding_history_list;

    public FundHoldHistoryAdapter(Context context) {
        this.mContext = context;
    }

    public void refreshData(List<FundAccountBean.FundHoldingList> fund_holding_history_list) {
        this.fund_holding_history_list = fund_holding_history_list;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View layout = LayoutInflater.from(mContext).inflate(R.layout.list_fund_hold_item, parent, false);
        return new MyViewHolder(layout);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        holder.fund_hold_item_title.setText(fund_holding_history_list.get(position).getFund_name());
        holder.tv_fund_hole_item_money_name.setText(fund_holding_history_list.get(position).getAmount_name());
        holder.tv_fund_hole_item_yesterday_name.setText(fund_holding_history_list.get(position).getDay_rate_name());
        holder.tv_fund_hole_item_flow_profit_name.setText(fund_holding_history_list.get(position).getFlow_profit_name());
        holder.tv_fund_item_money.setText(AbStrUtil.ChangeF3D(fund_holding_history_list.get(position).getMoney()));
        if (fund_holding_history_list.get(position).getFlow_profit().contains("-")) {
            holder.tv_fund_item_flow_profit.setText(AbStrUtil.ChangeF3D(fund_holding_history_list.get(position).getFlow_profit()));
        } else {
            holder.tv_fund_item_flow_profit.setText("+" + AbStrUtil.ChangeF3D(fund_holding_history_list.get(position).getFlow_profit()));
        }
//        holder.tv_fund_item_flow_profit.setText(fund_holding_history_list.get(position).getFlow_profit());
        if (fund_holding_history_list.get(position).getDay_rate().contains("-")) {
            if (fund_holding_history_list.get(position).getProfit_yesterday().contains("-")) {
                holder.tv_fund_item_profit_yesterday.setText(AbStrUtil.ChangeF3D(fund_holding_history_list.get(position).getProfit_yesterday()) + "(" + fund_holding_history_list.get(position).getDay_rate() + "% )");
            } else {
                holder.tv_fund_item_profit_yesterday.setText("+" + AbStrUtil.ChangeF3D(fund_holding_history_list.get(position).getProfit_yesterday()) + "(" + fund_holding_history_list.get(position).getDay_rate() + "% )");
            }
        } else {
            if (fund_holding_history_list.get(position).getProfit_yesterday().contains("-")) {
                holder.tv_fund_item_profit_yesterday.setText(AbStrUtil.ChangeF3D(fund_holding_history_list.get(position).getProfit_yesterday()) + "(+" + fund_holding_history_list.get(position).getDay_rate() + "%)");
            } else {
                holder.tv_fund_item_profit_yesterday.setText("+" + AbStrUtil.ChangeF3D(fund_holding_history_list.get(position).getProfit_yesterday()) + "(+" + fund_holding_history_list.get(position).getDay_rate() + "%)");
            }
        }
//        holder.tv_fund_item_profit_yesterday.setText(fund_holding_history_list.get(position).getProfit_yesterday());
        if (onItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick(holder.itemView, position);

                }
            });
        }
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    @Override
    public int getItemCount() {
        return fund_holding_history_list == null ? 0 : fund_holding_history_list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.fund_hold_item_title)
        TextView fund_hold_item_title;
        @Bind(R.id.tv_fund_hole_item_money_name)
        TextView tv_fund_hole_item_money_name;
        @Bind(R.id.tv_fund_hole_item_yesterday_name)
        TextView tv_fund_hole_item_yesterday_name;
        @Bind(R.id.tv_fund_hole_item_flow_profit_name)
        TextView tv_fund_hole_item_flow_profit_name;
        @Bind(R.id.tv_fund_item_money)
        TextView tv_fund_item_money;
        @Bind(R.id.tv_fund_item_profit_yesterday)
        TextView tv_fund_item_profit_yesterday;
        @Bind(R.id.tv_fund_item_flow_profit)
        TextView tv_fund_item_flow_profit;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}